package com.aaa.javaweb.dao.impl;

import com.aaa.javaweb.dao.TChukuDao;
import com.aaa.javaweb.entity.TChuku;
import com.aaa.javaweb.until.JDBCUntil;
import com.aaa.javaweb.until.TransBean;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public class TChukuDaoImpl implements TChukuDao {
    @Override
    public int addChuku(Map map) {
        String sql = "insert into t_chuku(rdate,uid,totalcount,jsr) values (now(),?,?,?)";
        return JDBCUntil.Updatekeys(sql,map.get("uid"),map.get("totalcount"),map.get("jsr"));
    }

    @Override
    public int addChukuDetail(List<Map> tableData, int chukuid) {
        String sql = "insert into t_chukudetail (id,ssid,count,chukukuid) values (null,?,?,?)";
        if (tableData != null) {
            int i = 0;
            for (Map map : tableData) {
                i += JDBCUntil.Updateout(sql, map.get("ssid"), map.get("count"), chukuid);
            }
            return i;
        }
        return 0;
    }

    @Override
    public int chukuAndAddBookCount(List<Map> tableData) {
        String sql = "update t_sheshi set count = count - ? where id = ? ";
        if (tableData != null) {
            int i = 0;
            for (Map map : tableData) {
                i += JDBCUntil.Updateout(sql, map.get("count"), map.get("ssid"));
            }
            return i;
        }
        return 0;
    }


    @Override
    public List<TChuku> getAllRuku(TChuku chuku) {
        StringBuilder sql = new StringBuilder("select r.rukuid,r.rdate,r.uid,r.totalcount,r.jsr,s.sname from t_chuku r left join t_staff s on r.uid = s.id where 1=1");
        if (chuku.getSname()!=null&&chuku.getSname()!=""){
            sql.append(" and s.sname like '%").append(chuku.getSname()).append("%'");
        }
        if (chuku.getJsr()!=null&&chuku.getJsr()!=""){
            sql.append(" and r.jsr like '%").append(chuku.getJsr()).append("%'");
        }
        if (chuku.getCreatedTime()!=null&&chuku.getCreatedTime()!=""){
            sql.append(" and r.rdate >= '").append(chuku.getCreatedTime()).append("'");
        }
        if (chuku.getEndTime()!=null&&chuku.getEndTime()!=""){
            sql.append(" and r.rdate <= '").append(chuku.getEndTime()).append("'");
        }
        sql.append(" order by r.rukuid desc limit ?,?");
        List<Map> maps = JDBCUntil.selectMap(sql + "", (chuku.getPage() - 1) * chuku.getLimit(), chuku.getLimit());
        return TransBean.populate(TChuku.class,maps);
    }

    @Override
    public long getCount(TChuku chuku) {
        StringBuilder sql = new StringBuilder("select count(*) c from t_chuku r left join t_staff s on r.uid = s.id where 1=1");
        if (chuku.getSname()!=null&&chuku.getSname()!=""){
            sql.append(" and s.sname like '%").append(chuku.getSname()).append("%'");
        }
        if (chuku.getJsr()!=null&&chuku.getJsr()!=""){
            sql.append(" and r.jsr like '%").append(chuku.getJsr()).append("%'");
        }
        if (chuku.getCreatedTime()!=null&&chuku.getCreatedTime()!=""){
            sql.append(" and r.rdate >= '").append(chuku.getCreatedTime()).append("'");
        }
        if (chuku.getEndTime()!=null&&chuku.getEndTime()!=""){
            sql.append(" and r.rdate <= '").append(chuku.getEndTime()).append("'");
        }
        sql.append(" order by r.rukuid desc ");
        List<Map> maps = JDBCUntil.selectMap(sql + "");
        if (maps.size()>0){
            return (long) maps.get(0).get("c");
        }
        return 0;
    }

    @Override
    public List<Map> getAllRukuDetailByRukuId(String rukuid) {
        String sql = "select r.id,r.chukukuid,r.ssid,r.count,b.name,b.gys from t_chukudetail r join t_sheshi b on r.ssid = b.id where r.chukukuid = ?";
        return JDBCUntil.selectMap(sql, rukuid);
    }

    @Override
    public int addTFChuku(Integer id, int xHcount) {
        String sql = "insert into t_chuku(rdate,uid,totalcount,jsr) values (now(),?,?,'Cleaner')";
        return JDBCUntil.Updatekeys(sql,id,xHcount);
    }

    @Override
    public int addTFChukuDetail(int rukuid) {
        String sql = "insert into t_chukudetail (id,ssid,count,chukukuid) values (null,?,1,?)";
        String s = "select id from t_sheshi where typeid=1 and count>0 ";
        List<Map> maps = JDBCUntil.selectMap(s);
            int i = 0;
            for (Map map : maps) {
                i += JDBCUntil.Updateout(sql, map.get("id"), rukuid);
            }
            return i;
    }

    @Override
    public int tfchukuAndAddBookCount() {
        String sql = "update t_sheshi set count = count - 1 where id = ? ";
        String s = "select id from t_sheshi where typeid=1 and count>0 ";
        List<Map> maps = JDBCUntil.selectMap(s);

            int i = 0;
            for (Map map : maps) {
                i += JDBCUntil.Updateout(sql, map.get("id"));
            }
            return i;

    }
}
